package com.test.daily.leetcode.y2022.m04.day0419.v0013;

import java.util.HashMap;

/**
 * @author Tom on 2022/4/19
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int romanToInt(String s){
        int ans = 0;
        int N  = s.length();
        HashMap<Character, Integer> label = new HashMap<Character, Integer>(){{
                put('I', 1);
                put('V',5);
                put('X',10);
                put('L', 50);
                put('C',100);
                put('D',500);
                put('M',1000);

        }};
        for (int i = 0; i < N; i++) {
            if(i < N -1 && (label.get(s.charAt(i)) < label.get(s.charAt(i+1)))){
                ans -= label.get(s.charAt(i));
            } else {
                ans += label.get(s.charAt(i));
            }
        }
        return ans;
    }
}
